package com.carrental.api.ums.controller;

import com.carrental.common.response.CommonResult;
import com.carrental.api.ums.domain.bo.UmsLoginBo;
import com.carrental.api.ums.service.IUmsUserLoginService;
import com.carrental.common.utils.StpUmsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LiangYi
 * @since 2022/3/23 21:50
 */
@Api(tags = "会员登录")
@RestController
@RequestMapping("/ums")
public class UmsLoginController {
    @Autowired
    private IUmsUserLoginService loginService;

    @ApiOperation("登录")
    @PostMapping("/login")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Map<String,Object>> login(@RequestBody UmsLoginBo loginUser){
        Map<String, Object> userInfo = new HashMap<>(8);
        userInfo.put("token",loginService.login(loginUser));
        return CommonResult.success(userInfo);

    }

    @ApiOperation("退出登录")
    @PostMapping("/logout")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> logout(){
        StpUmsUtil.logout();
        return CommonResult.success();

    }

    @ApiOperation("获取验证码")
    @GetMapping("/code")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> login(@RequestParam String telephone){
        return CommonResult.success(loginService.sendCode(telephone));
    }
}
